use32

IMAGEBASE = 400000h

org IMAGEBASE

img_start:
dw "MZ"                                                                                                                            ; e_magic
dw 0                                                                                                                               ; e_cblp
dw "PE", 0                                                                                          ; Signature                    ; e_cp
                                                                                                                                   ; e_crlc
dw 014ch                                                                                            ; Machine                      ; e_cparhdr
dw 1                                                                                                ; NumberOfSections             ; e_minalloc
dd 0                                                                                                ; TimeDateStamp                ; e_maxalloc
                                                                                                                                   ; e_ss
dd 0                                                                                                ; PointerToSymbolTable         ; e_sp
                                                                                                                                   ; e_csum
dd 0                                                                                                ; NumberOfSymbols              ; e_ip
                                                                                                                                   ; e_cs
dw SECTION_TABLE - OPTION_HEADER                                                                    ; SizeOfOptionalHeader         ; e_lfarlc
dw 0102h                                                                                            ; Characteristics              ; e_ovno
OPTION_HEADER: dw 010bh                                                                             ; Magic                        ; e_res[0]
db 0                                                                                                ; MajorLinkerVersion           ; e_res[1]
db 0                                                                                                ; MinorLinkerVersion
dd 0                                                                                                ; SizeOfCode                   ; e_res[2]
                                                                                                                                   ; e_res[3]
dd 0                                                                                                ; SizeOfInitializedData        ; e_oemid
                                                                                                                                   ; e_oeminfo             
dd 0                                                                                                ; SizeOfUninitializedData      ; e_res2[0]
                                                                                                                                   ; e_res2[1]
dd code_start - img_start                                                                           ; AddressOfEntryPoint          ; e_res2[2]
                                                                                                                                   ; e_res2[3]
dd 0                                                                                                ; BaseOfCode                   ; e_res2[4]
                                                                                                                                   ; e_res2[5]
dd 0                                                                                                ; BaseOfData                   ; e_res2[6]
                                                                                                                                   ; e_res2[7]
dd 400000h                                                                                          ; ImageBase                    ; e_res2[8]
                                                                                                                                   ; e_res2[9]
dd 4                                                                                                ; SectionAlignment             ; e_lfanew
dd 4                                                                                                ; FileAlignment
dw 0                                                                                                ; MajorOperatingSystemVersion
dw 0                                                                                                ; MinorOperatingSystemVersion
dw 0                                                                                                ; MajorImageVersion
dw 0                                                                                                ; MinorImageVersion
dw 4                                                                                                ; MajorSubsystemVersion
dw 0                                                                                                ; MinorSubsystemVersion
dd 0                                                                                                ; Win32VersionValue
dd img_end - img_start                                                                              ; SizeOfImage
dd code_start - img_start                                                                           ; SizeOfHeaders
dd 0                                                                                                ; CheckSum
dw 3                                                                                                ; console; Subsystem
dw 0                                                                                                ; DllCharacteristics
dd 0                                                                                                ; SizeOfStackReserve
dd 0                                                                                                ; SizeOfStackCommit
dd 0                                                                                                ; SizeOfHeapReserve
dd 0                                                                                                ; SizeOfHeapCommit
dd 0                                                                                                ; LoaderFlags
dd 2                                                                                                ; NumberOfRvaAndSizes
dd 0                                                                                                ; EXPORT VirtualAddress
dd 0                                                                                                ; EXPORT Size
dd imp_start - img_start                                                                            ; IMPORT VirtualAddress
dd imp_end - imp_start                                                                              ; IMPORT Size

SECTION_TABLE: db ".text", 0, 0, 0          ; Name[8]
dd code_end - code_start                    ; VirtualSize
dd code_start - img_start                   ; VirtualAddress
dd code_end - code_start                    ; SizeOfRawData
dd code_start - img_start                   ; PointerToRawData
dd 0                                        ; PointerToRelocations
dd 0                                        ; PointerToLinenumbers
dw 0                                        ; NumberOfRelocations
dw 0                                        ; NumberOfLinenumbers
dd 0                                        ; Characteristics
align 4

code_start:
argc = -4h
argv = -8h
enter 8h, 0
xor edi, edi
call [GetForegroundWindow]
test eax, eax
jz _error

mov ebx, eax
lea esi, [edi - 1]
lea eax, [ebp + argc]
lea ecx, [ebp + argv]
mov [eax], edi
push eax
push edi
push eax
push ecx
push eax
call [__getmainargs]

mov eax, [ebp + argv]
mov eax, [eax + 4]
test eax, eax
jz @f
push eax
call [atoi]
mov esi, eax
push eax
push edi
WM_INPUTLANGCHANGEREQUEST = 50h
push WM_INPUTLANGCHANGEREQUEST
push ebx
call [SendMessage]

@@:
push edi
push ebx
call [GetWindowThreadProcessId]
push eax
call [GetKeyboardLayout]
and eax, 0FFFFh

test esi, esi
jl @f
cmp eax, esi
jne _error
jmp _success

@@:
push eax
call @f
db "%u", 10, 0
@@:
call [printf]

_success:
xor eax, eax
jmp _end
_error:
lea eax, [edi + 1]
_end:
leave
ret

imp_start:

dd user32_iat - img_start; OriginalFirstThunk
dd 0 ; TimeDateStamp
dd 0 ; ForwarderChain
dd user32 - img_start; Name
dd user32_iat - img_start; FirstThunk

dd msvcrt_iat - img_start; OriginalFirstThunk
dd 0 ; TimeDateStamp
dd 0 ; ForwarderChain
dd msvcrt - img_start; Name
dd msvcrt_iat - img_start; FirstThunk

dd 5 dup(0)

imp_end:

user32_iat:
GetForegroundWindow       dd _GetForegroundWindow       - img_start
GetWindowThreadProcessId  dd _GetWindowThreadProcessId  - img_start
GetKeyboardLayout         dd _GetKeyboardLayout         - img_start
SendMessage               dd _SendMessage               - img_start
dd 0

msvcrt_iat:
printf                    dd _printf                    - img_start
atoi                      dd _atoi                      - img_start
__getmainargs             dd ___getmainargs             - img_start

dd 0

user32:                    db "user32.dll",0
_GetForegroundWindow:      dw 0
                           db "GetForegroundWindow",0
_GetWindowThreadProcessId: dw 0
                           db "GetWindowThreadProcessId",0
_GetKeyboardLayout:        dw 0
                           db "GetKeyboardLayout",0
_SendMessage:              dw 0
                           db "SendMessageA",0

msvcrt:                    db "msvcrt.dll",0
_printf:                   dw 0
                           db "printf",0
_atoi:                     dw 0
                           db "atoi",0
___getmainargs:            dw 0
                           db "__getmainargs",0

align 4
img_end:
code_end:
